package com.cnkvha.trackcontestmgmt;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class ResourceUtil {

    public static boolean saveResource(String fileName, boolean overwrite) {
        File f = new File(fileName);
        if(f.exists() && !overwrite) return true;
        try {
            byte[] buffer = new byte[2048];
            InputStream is = ResourceUtil.class.getResourceAsStream("/" + fileName);
            FileOutputStream fos = new FileOutputStream(f);
            int r;
            while((r = is.read(buffer)) != -1) {
                fos.write(buffer, 0, r);
            }
            fos.flush();
            fos.close();
            is.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

}
